package io.renren.modules.frt.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.modules.frt.entity.DistrictEntity;
import io.renren.modules.frt.service.DistrictService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.common.utils.Constant;
import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 行政区管理
 *
 * @author qibing.huang
 * @email abingtech@163.com
 * @date 2018-11-18 12:45:12
 */
@RestController
@RequestMapping("frt/district")
public class DistrictController extends AbstractController{
    @Autowired
    private DistrictService districtService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("frt:district:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = districtService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
	 * 行政区列表
	 */
	@GetMapping("/select")
	@RequiresPermissions("frt:district:select")
	public R select(@RequestParam Map<String, Object> params){
		Map<String, Object> map = new HashMap<>();
		map.put("level", params.get("level"));
		map.put("parent_code", params.get("parentCode"));
		
		List<DistrictEntity> allList = new ArrayList<>();
		if("history".equals(MapUtils.toStr(params, "flag"))) {
			DistrictEntity china = districtService.selectOne(new EntityWrapper<DistrictEntity>().eq("level", 0));
			allList.add(china);
		}
		
		List<DistrictEntity> list = districtService.selectByMap(map);
		allList.addAll(list);
		
		return R.ok().put("list", allList);
	}

    /**
     * 信息
     */
    @RequestMapping("/info/{districtId}")
    @RequiresPermissions("frt:district:info")
    public R info(@PathVariable("districtId") Long districtId){
        DistrictEntity district = districtService.selectById(districtId);

        return R.ok().put("district", district);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("frt:district:save")
    public R save(@RequestBody DistrictEntity district){
		districtService.insert(district);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("frt:district:update")
    public R update(@RequestBody DistrictEntity district){
		districtService.updateById(district);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("frt:district:delete")
    public R delete(@RequestBody Long[] districtIds){
		districtService.deleteBatchIds(Arrays.asList(districtIds));

        return R.ok();
    }

}
